home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / educate / wordy402.zip / UNSCR.C < prev    next >
C/C++ Source or Header  |  1995-12-16  |  5KB  |  210 lines

  1. /**************************************************************************/
  2. /*                           UNSCRAMBLE Utility                        */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                        3425 Chestnut Ridge Rd.                        */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /**************************************************************************/
  13.  
  14.  
  15. /**********************************WORDTEST********************************/
  16. /*       Function tests if word is constructible from Letterset            */
  17. /*                 Args in: char *letterset, char *word                          */
  18. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  19. /**************************************************************************/
  20.  
  21. #include <conio.h>
  22. #include "srch.h"
  23.  
  24.  
  25. #define FILE_OPENING_ERROR 3
  26. #define FILENAME_MAXLEN 40
  27. #define CR "\n"
  28. #define FILE_SUFFIX ".wds"
  29. #define MAXLEN 30
  30. #define LINE_LEN 80
  31. #define NOARGS 1
  32. #define INCREMENT 1
  33. #define SPACE ' '
  34.  
  35. #define BUFFERSIZE 16384
  36.  
  37. char ad[] =
  38. "UNSCRAMBLE utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  39.  
  40.  
  41.  
  42. void getword( char *letter_set, size_t w_len, char *filenam );
  43. void center( char *strng );
  44.  
  45. typedef enum { FALSE, TRUE } Boolean;
  46.  
  47. void main( int argc, char **argv )
  48. {
  49.  
  50.    char letterset [MAXLEN],
  51.         filenam [FILENAME_MAXLEN];
  52.  
  53.      if( argc == NOARGS )
  54.         {
  55.         clrscr();
  56.         puts( "Enter a LETTERSET to test ... " );
  57.         gets( letterset );
  58.      strcpy( filenam, "word.lst" );
  59.      getword( letterset, strlen( letterset ), filenam );
  60.         }
  61.  
  62.   else   
  63.       if( argc == NOARGS + 1 )
  64.          {
  65.          strcpy( filenam, "word.lst" );
  66.          strcpy( letterset, *( argv + 1) );
  67.          getword( letterset, strlen( letterset ), filenam );
  68.          }
  69.      else
  70.         {
  71.         strcpy( letterset, *( argv + 1 ) );
  72.      strcpy( filenam, *( argv + 2 ) );
  73.         getword( letterset, strlen( letterset ), filenam );
  74.         }
  75. }
  76.  
  77.  
  78.  
  79. Boolean wordtest( char *letterset, char *word )
  80. {
  81.     Boolean error_flag = TRUE;
  82.     static char dup_lset[ MAXLEN ];
  83.     register char *letpos;
  84.  
  85. //     dup_lset = strdup( letterset );
  86.      strcpy( dup_lset, letterset );
  87.          
  88.         while( *word )
  89.             {
  90.             if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
  91.                 *letpos = '*';     //As long as letter contained...
  92.             else
  93.                 { error_flag = FALSE; break; } //test fails (not contained)
  94.             }
  95.  
  96.         return( error_flag );
  97. }
  98.  
  99. /*************************************************************/
  100. void getword( char *letter_set, size_t w_len, char *filename )
  101. {
  102.  
  103.     char    l_set [ MAXLEN ],
  104.         word [ MAXLEN ],
  105.         tempstr [ MAXLEN + 1 ],
  106.         bar [ LINE_LEN + 1 ],
  107.         double_bar [ LINE_LEN + 1 ],
  108.    ts [ MAXLEN ];
  109.  
  110.     FILE *fptr;
  111.     long wcount = 0L;
  112.  
  113.        memset( bar, '-', LINE_LEN );
  114.        *( bar + LINE_LEN ) = NULL;
  115.        memset( double_bar, '=', LINE_LEN );
  116.        *( double_bar + LINE_LEN ) = NULL;
  117.  
  118.        /*************opening credits*************/
  119.        clrscr();
  120.        printf( double_bar );
  121.        strcpy( tempstr, ad );
  122.        center ( tempstr );
  123.        printf( tempstr );
  124.        printf( CR );
  125.        printf( double_bar );
  126.        printf( CR );
  127.        /****************************************/
  128.  
  129.  
  130.        strcpy ( l_set, letter_set );
  131.        strcat ( letter_set, CR );
  132.  
  133.  
  134.        if( !( fptr = fopen( filename, "rt" ) ) )
  135.          {
  136.          printf( "\7\7\7Cannot open wordfile %s!", filename );
  137.          exit( FILE_OPENING_ERROR );
  138.          }
  139.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  140.          exit( FILE_OPENING_ERROR );
  141.  
  142.        /**************'Wait' Message************/
  143.        printf( CR CR );
  144.        printf( "WORKING...\n\n" );
  145.        printf( "This will take a few seconds...\n" );
  146.        printf( "Please be patient.\n\n" );
  147.        printf( "Now searching 100,000+ word file for possible solutions...\n\n" );
  148.        /*****************************************/
  149.  
  150.  
  151.  
  152.  
  153.  
  154.        sprintf( tempstr, "Word(s) unscrambled from: %s\n", strupr( l_set ) );
  155.        center( tempstr );
  156.        printf( double_bar );
  157.        printf( tempstr );
  158.        printf( double_bar );
  159.        printf( CR );
  160.  
  161.  
  162.          /*********************Main Loop*************/     
  163.           while( fgets( word, MAXLEN, fptr ) != NULL )
  164.  
  165.             if( wordtest( letter_set, word ) )
  166.                if( strlen( word ) == w_len + INCREMENT )
  167.                  {
  168.                  printf( "%s", word );
  169.                  wcount++;
  170.                      }
  171.  
  172.       if( wcount == INCREMENT )
  173.          strcpy( ts, "word" );
  174.       else
  175.          strcpy( ts, "words" );
  176.  
  177.           /*******************************************/
  178.  
  179.           printf( bar );
  180.           sprintf( tempstr, "%ld %s can be unscrambled from %s.",
  181.                  wcount, ts, l_set );
  182.           center( tempstr );              
  183.           printf( tempstr );
  184.           printf( "\n\n" );
  185.  
  186.           center( ad );
  187.           printf( ad );
  188.       printf( "\7\n\n" );
  189.  
  190.           fcloseall();
  191.  
  192.  
  193. }
  194.  
  195.  
  196.  
  197. void center( char *str )
  198. {
  199.    int padding;
  200.    char st [ LINE_LEN + INCREMENT ];
  201.  
  202.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  203.      memset( st, SPACE, padding );
  204.      *( st + padding ) = NULL;  //Terminate string
  205.      strcat( st, str );
  206.      strcpy( str, st );
  207.  
  208.      return;
  209. }
  210.